var _util = _interopRequireDefault(require("../../utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var HEIGHT = 894, cacheUrl = {};

Component({
    properties: {
        src: {
            type: String,
            observer: function(t) {
                this.set(t);
            }
        },
        zIndex: {
            type: Number,
            value: 500
        }
    },
    data: {
        shown: !1,
        loading: !0,
        url: ""
    },
    methods: {
        set: function(t) {
            var e = this;
            this.setData({
                url: t
            }), cacheUrl[t] || (this.setData({
                loading: !0
            }), this.timer = setTimeout(function() {
                e._onImgComplete();
            }, 5e3));
        },
        show: function() {
            this.setData({
                shown: !0
            });
        },
        hide: function() {
            this.setData({
                shown: !1
            }), this.triggerEvent("cancel", {}, {});
        },
        _onHide: function() {
            this.hide();
        },
        _onImgComplete: function(t) {
            var e = {
                loading: !1
            };
            if (t && t.detail) {
                var i = t.detail, n = i.width, a = i.height;
                e.width = n / (a / HEIGHT), cacheUrl[this.data.url] = !0;
            }
            this.setData(e);
        },
        _onShowImg: function() {
            wx.previewImage({
                urls: [ this.data.url ]
            }), this.triggerEvent("click", {
                type: "img"
            });
        },
        _onSave: function() {
            var t = this;
            wx.showLoading({
                title: "保存中...",
                mask: !0
            }), _util.default.dowload(this.data.url).then(function(t) {
                return _util.default.writePhotosAlbum(t);
            }).then(function() {
                t.hide(), wx.showToast({
                    icon: "success",
                    title: "已保存到手机"
                }), t.triggerEvent("click", {
                    type: "save"
                });
            }).catch(wx.hideLoading);
        }
    }
});